﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BLL;
using Model;
using System.Data.SqlClient;
using Common;
using System.Data;
using System.Threading;
namespace WebPortal.Controllers
{
    public class SysViewApproveController : ViewInfo
    {
        private static ManualResetEvent[] resetEvents;

        /// <summary>
        /// 保存视图，提交视图审核
        /// </summary>
        /// <param name="id">视图组id</param>
        /// <param name="collection">提交数据</param>
        [HttpPost]
        public void AddApprove(string dbName, string id, FormCollection collection)
        {
            try
            {
                if (collection.Count > 0)
                {
                    LoginInfo loginInfos = base.loginInfo();
                    if (loginInfos != null)
                    {
                        loginInfos.DBName = dbName;
                        string[] arrIDs = collection[0].ToString().Split(',');
                        //resetEvents = new ManualResetEvent[arrIDs.Length];
                        int i = 0;
                        foreach (string vid in arrIDs)//vid=视图id
                        {
                            //resetEvents[i] = new ManualResetEvent(false);
                            i++;
                            Dictionary<string, object> appInfo = new Dictionary<string, object>();
                            appInfo.Add("vid", vid);
                            appInfo.Add("id", id);
                            //多进程模式
                            try
                            {
                                Thread t = new Thread(new ParameterizedThreadStart(DoWork));
                                t.Start(new object[] { dbName, appInfo });
                            }
                            catch { }
                            //调试模式
                            //DoWork((object)appInfo);

                        }
                        // WaitHandle.WaitAll(resetEvents);
                    }
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// 执行视图审核
        /// </summary>
        /// <param name="o"></param>
        //public void DoWork(string dbName, object o)
        public void DoWork(object obj)
        {
            object[] objs = obj as object[];
            string dbName = string.Empty;
            if (objs.Length > 1)
                dbName = objs[0].ToString();
            object o = objs.Length > 1 ? objs[1] : null;
            Dictionary<string, object> appInfo = (Dictionary<string, object>)o;

            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysViewGroupBLL bll = new SysViewGroupBLL(loginInfos);
            bll.CheckApprove(appInfo);


        }
        /// <summary>
        /// 视图审核列表
        /// </summary>
        /// <param name="collection"></param>
        /// <returns></returns>
        public ActionResult List(FormCollection collection, string dbName)
        {
            Dictionary<string, string> state = collection["state"].FromJSon<Dictionary<string, string>>();
            if (state != null)
            {
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
                IDataParameter[] queryParas = new SqlParameter[state.Count + 3];
                int index = 0;
                foreach (string item in state.Keys)
                {
                    queryParas[index] = new SqlParameter("@" + item, state[item]);
                    index += 1;
                }
                queryParas[index] = new SqlParameter("@Rows", collection["rows"].ToString());
                queryParas[index + 1] = new SqlParameter("@PageNum", collection["page"].ToString());
                queryParas[index + 2] = new SqlParameter("@SysCode", loginInfos.ETLTargetCode);
                List<SysViewApprove> list = bll.ApproveList(queryParas);
                int count = bll.ApproveListCount(queryParas);
                Dictionary<String, Object> jsonDict = new Dictionary<String, Object>();
                jsonDict.Add("total", count);
                jsonDict.Add("rows", list);
                return Json(jsonDict);
            }
            else
                return View();
        }
        /// <summary>
        /// 视图审核信息项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult GetItem(string dbName, string id)
        {
            LoginInfo loginInfos = base.loginInfo();
            loginInfos.DBName = dbName;
            SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
            string msg = bll.GetAuditDetail(id);
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("AuditResult", msg);
            return Json(dict);
        }

    }
}
